package com.mitcub.panda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mitcub.panda.base.BaseQuery;
import com.mitcub.panda.base.BaseServiceImpl;
import com.mitcub.panda.entity.Admin;
import com.mitcub.panda.mapper.AdminMapper;
import com.mitcub.panda.query.AdminQuery;
import com.mitcub.panda.service.IAdminService;
import com.mitcub.panda.utils.RespResult;
import com.mitcub.panda.vo.AdminVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.apache.commons.codec.digest.DigestUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 * @项目名称: panda
 * @类名称: AdminServiceImpl
 * @类描述: 系统人员 服务实现类
 * @作者: tangloong
 * @创建时间: 2021/11/2 17:22
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private AdminMapper adminMapper;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public RespResult getList(BaseQuery query) {
        AdminQuery adminQuery = (AdminQuery) query;
        // 查询条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        // 人员姓名/手机号
        if (!StringUtils.isEmpty(adminQuery.getKeywords())) {
            queryWrapper.like("t1.realname", adminQuery.getKeywords())
                    .or().like("t1.username", adminQuery.getKeywords())
                    .or().like("t1.mobile", adminQuery.getKeywords());
        }
        // 性别:1男 2女 3保密
        if (adminQuery.getGender() != null) {
            queryWrapper.eq("t1.gender", adminQuery.getGender());
        }
        // 状态：1正常 2禁用
        if (adminQuery.getStatus() != null) {
            queryWrapper.eq("t1.status", adminQuery.getStatus());
        }
        queryWrapper.eq("t1.mark", 1);
        queryWrapper.orderByDesc("t1.id");

        // 查询数据
        Page<Admin> page = new Page<>(adminQuery.getPage(), adminQuery.getLimit());
        IPage<AdminVo> data = adminMapper.selectPageExt(page, queryWrapper);
        return RespResult.success("操作成功", data.getRecords(),data.getTotal());
    }



    /**
     * 添加或编辑记录
     * @param entity 实体对象
     * @return
     */
    @Override
    public RespResult edit(Admin entity) {
        if (!StringUtils.isBlank(entity.getPassword())) {
            entity.setPassword(DigestUtils.md5Hex(entity.getPassword()));
        } else {
            entity.setPassword(null);
        }

        if(null == entity.getRoleId()){
            RespResult.error("角色不能为空");
        }
        if(entity.getRoleId()==1){
            if(entity.getId()!=1){
                RespResult.error("该账户不能设置为超级管理员账户权限");
            }
        }

        return super.edit(entity);
    }


    /**
     * 根据用户名获取人员
     * @param username 用户名
     * @return
     */
    @Override
    public Admin getAdminByUsername(String username) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("mark", 1);
        Admin user = adminMapper.selectOne(queryWrapper);
        return user;
    }
}